package com.ysu.demo.controller;


import com.ysu.demo.VO.ResultVO;
import com.ysu.demo.enums.ExceptionEnum;
import com.ysu.demo.enums.IncomeState;
import com.ysu.demo.exception.BaseException;
import com.ysu.demo.from.income.IncomeChangeMoneyFrom;
import com.ysu.demo.from.income.IncomeChangeStateFrom;
import com.ysu.demo.from.income.IncomeCreateFrom;
import com.ysu.demo.service.IIncomeService;
import com.ysu.demo.utlis.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lhw
 * @since 2019-08-21
 */
@RestController
@RequestMapping("/income")
public class IncomeController {
    @Autowired
    IIncomeService incomeService;

    @PostMapping("/create")
    public ResultVO add(@RequestBody @Valid IncomeCreateFrom incomeCreateFrom,
                        BindingResult bindingResult){

        if(bindingResult.hasErrors()){
            throw new BaseException(ExceptionEnum.FAIL_CHECK);
        }

        incomeService.create(incomeCreateFrom);

        return ResultUtil.TrueResult();
    }

    @PostMapping("/change/money")
    public ResultVO changeMoney(@RequestBody @Valid IncomeChangeMoneyFrom from,
                                BindingResult bindingResult){

        if(bindingResult.hasErrors()){
            throw new BaseException(ExceptionEnum.FAIL_CHECK);
        }

        incomeService.rewriteMoney(from.getId(),from.getMoney());

        return ResultUtil.TrueResult();
    }

    @PostMapping("/changeto/goodpeople")
    public ResultVO changeArgue(@RequestBody @Valid IncomeChangeStateFrom from,
                                BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            throw new BaseException(ExceptionEnum.FAIL_CHECK);
        }

        incomeService.changeState(IncomeState.WAIT_ARGUE,
                IncomeState.WAIT_GOODPEOPLE,
                from.getId(),
                from.getUrl());

        return ResultUtil.TrueResult();
    }

    @PostMapping("/changeto/foundation")
    public ResultVO changeGoodPeople(@RequestBody @Valid IncomeChangeStateFrom from,
                                BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            throw new BaseException(ExceptionEnum.FAIL_CHECK);
        }

        incomeService.changeState(IncomeState.WAIT_GOODPEOPLE,
                IncomeState.WAIT_JJ,
                from.getId(),
                from.getUrl());

        return ResultUtil.TrueResult();
    }

    @PostMapping("/changeto/waitmoney")
    public ResultVO changeJJ(@RequestBody @Valid IncomeChangeStateFrom from,
                                BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            throw new BaseException(ExceptionEnum.FAIL_CHECK);
        }

        incomeService.changeState(IncomeState.WAIT_JJ,
                IncomeState.WAIT_GET,
                from.getId(),
                from.getUrl());

        return ResultUtil.TrueResult();
    }

    @PostMapping("/changeto/getmoney")
    public ResultVO changeWaitGet(@RequestBody @Valid IncomeChangeStateFrom from,
                                BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            throw new BaseException(ExceptionEnum.FAIL_CHECK);
        }

        incomeService.changeState(IncomeState.WAIT_GET,
                IncomeState.ALREADY_GET,
                from.getId(),
                from.getUrl());

        return ResultUtil.TrueResult();
    }

    @PostMapping("/changeto/over")
    public ResultVO changeAlreadyGet(@RequestBody @Valid IncomeChangeStateFrom from,
                                BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            throw new BaseException(ExceptionEnum.FAIL_CHECK);
        }

        incomeService.changeState(IncomeState.ALREADY_GET,
                IncomeState.OVER,
                from.getId(),
                from.getUrl());

        return ResultUtil.TrueResult();
    }

}
